import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import Qt.database.sqlite 1.0
import "../mainmenu"

Rectangle {
    /*width: 800;*/ /*height: 350*/
    property string name: ""
    property string sql: "select Text from Instructions where Type = \'Common\' and Title = \'MoveC\'"
    property var codelist: new Array

    signal clickedCodes(int index, string code)
    function showExtra(flag, title)
    {
        name = title.substring(0, title.length -1)
        name = name.trim()
        //title.trim(' ')
        console.log(name)
        if(flag === 0){
            if(name === qsTr("添加指令"))
            {
                addinstruct.visible = true
                edit.visible = false
                debug.visible = false
            }
            else if(name === qsTr("编辑"))
            {
                edit.visible = true
                addinstruct.visible = false
                debug.visible = false
            }
            else if(name === qsTr("调试"))
            {
                debug.visible = true
                addinstruct.visible = false
                edit.visible = false
            }
        }
        else
        {
            if(name === qsTr("添加指令"))
            {
                addinstruct.visible = false
            }
            else if(name === qsTr("编辑"))
            {
                edit.visible = false
            }
            else if(name === qsTr("调试"))
            {
                debug.visible = false
            }
        }
    }

    color: "white"
    border.color: "black"; border.width: 1;

    SqliteDB{
        id:db;
    }

    AddInstruct{
       // color: "blue"
        id: addinstruct
        visible: false
        width: parent.width*4/9
        anchors{
            right: parent.right; top: parent.top; bottom: parent.bottom
        }

        onInsertInstruction: {         //两个参数：string instructClass, string instruction
            db.connectDB();
            var code = db.getAData("select Text from Instructions where Type = '"+instructClass+
                               "' and Title = '"+ instruction +"'" )
            db.closeDB()
            listview.addOne(code)
        }
    }

    EditPanel{
        id: edit
        visible: false
        width: parent.width*4/9
        anchors{
            right: parent.right; top: parent.top; bottom: parent.bottom
        }
    }

    DebugPanel{
        id: debug
        visible: false
        width: parent.width*4/9
        anchors{
            right: parent.right; top: parent.top; bottom: parent.bottom;
        }
    }

    Component{
        id: propaneldelegate;
        Item{
            id: wrapper
            width: parent.width;
            height: 60

            MouseArea{
                anchors.fill: parent;
                onClicked: {
                    wrapper.ListView.view.currentIndex = index
//                    clickedCodes(1,"")
                    }
            }

            RowLayout{
                anchors.left: parent.left
                anchors.top: parent.top
                anchors.verticalCenter: parent.verticalCenter
                spacing: 8
                Text{
                    id: number
                    text: Rows
                    color: "black"
                    font.pixelSize: wrapper.ListView.isCurrentItem?22:18
                    horizontalAlignment: Text.AlignHCenter
                    Layout.preferredWidth: 50
                   // Layout.preferredHeight: 20
                }
                Text{
                    id: code
                    text: Codes
                    color: "green"
                    font.pixelSize: wrapper.ListView.isCurrentItem?40:30
                    Layout.fillWidth: true
                    MouseArea{
                        onClicked: {
                            console.log("clicked the row")
                            moveC.visible = true
                        }
                    }
                }
//                TextInput{
//                    id: code
//                    text: Codes
//                    color: "green"
//                    font.pixelSize: wrapper.ListView.isCurrentItem?55:44
//                    Layout.fillWidth: true
//                    selectByMouse: true
//                    mouseSelectionMode: TextInput.SelectWords
//                    selectionColor: "lightblue"
//                    readOnly: true
//                }

            }
        }
    }

    ListModel{
        id: datamodel
//        ListElement{
//            Rows: "1"
//            Codes: "PROC main()"
//        }
//        ListElement{
//            Rows: "2"
//            Codes: "MoveC *, *, v1000, z100, tool1"

//        }
    }

    ListView{
        id: listview
        property int rowCount: 1
        anchors.fill: parent
        delegate: propaneldelegate
        model: datamodel

        function addOne(data){
            model.insert(currentIndex+1, {"Rows": (currentIndex==-1?rowCount:currentIndex+2).toString(), "Codes": data})
            currentIndex++;
            for(var i = currentIndex + 1; i< model.count; i++)
            {
                model.setProperty(i, "Rows", (i+1).toString())
            }

//            model.append({"Rows": rowCount.toString(), "Codes": data})
            rowCount ++;
        }

        function removeOne(index, count)
        {
            model.remove(index, count)
            rowCount --;
            model.move(index+1, index, model.count - index);
        }

        focus: true
        highlight: Rectangle{
            color: "lightblue"
            height: parent.height
        }
    }

//    Text{
//        id: programmingpanel;
//        anchors.fill: parent

//        text: "PROC main()\n\tMoveC *, *, v1000, z100, tool1"
//    }
}
